package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.CommonResult;
import org.example.pojo.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {
    //获取服务的url
    @Autowired
    RestTemplate restTemplate;

    //记录服务提供方的主机+端口号        根据服务名称调用
    public static final String URL = "http://cloud-payment-service";

    //调用服务提供方   查询订单信息的方法
    @GetMapping("consumer/payment/{id}")
    public CommonResult<Payment> findOrderById(@PathVariable Long id){
        CommonResult commonResult = this.restTemplate.getForObject(URL + "/payment/" + id, CommonResult.class);
        return commonResult;
    }


    //新增的方法
    @PostMapping("consumer/payment/create")
    public CommonResult<Payment> addOrder(@RequestBody Payment payment){
        /**
         * 注意:使用restTemplate使用POST请求服务提供方，携带的参数，在底层会转换成一个json格式的字符串。
         */
        CommonResult commonResult = this.restTemplate.postForObject(URL + "/payment/create", payment, CommonResult.class);
        return commonResult;
    }


}
//  消费方：
//  localhost/consumer/payment/1
//  localhost/consumer/payment/create

// 提供方：
//  localhost:8001/payment/1  查询
//  localhost:8001/payment/create  新增